#include <service/FileSystemService.h>
#include <cppwinks/mysql/exception/SQLException.h>

#include <fstream>
#include <sstream>
#include <iostream>

FileSystemService *file_system_service = new FileSystemService;

using namespace std;

// 文件读取
std::string FileSystemService::readFile(const std::string &filePath)
{
    // 读取文件内容
    ifstream inFile(filePath, ios::binary);
    if (!inFile)
    {
        cout << "文件不存在: " << filePath << endl;
        throw runtime_error("文件不存在");
    }
    // 读取文件内容到字符串
    stringstream buffer;
    buffer << inFile.rdbuf();
    string fileContent = buffer.str();
    inFile.close();
    return fileContent;
}

// 文件写入
void FileSystemService::writeFile(const std::string &filePath, const std::string &fileContent)
{
    // 保存文件到本地
    ofstream outFile(filePath, ios::binary);
    if (!outFile)
    {
        cout << "Failed to open file for writing: " << filePath << endl;
        throw runtime_error("无法打开文件进行写入");
    }
    outFile.write(fileContent.data(), fileContent.size());
    outFile.close();
    cout << "打印调试信息: " << filePath << endl; //
}